
package net.atremmedia.gwt.actionjava.examples.bitmap.samples;

import net.atremmedia.gwt.actionjava.examples.AJExample;
import net.atremmedia.gwt.actionjava.examples.frame.AJExampleOverview;
import actionjava.anim.Anim;
import actionjava.anim.config.Manifest;
import actionjava.display.Bitmap;
import actionjava.display.BitmapData;
import actionjava.display.Graphics;
import actionjava.display.Shape;
import actionjava.display.Sprite;
import actionjava.geom.Color;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.ui.RootPanel;

public class BitmapExample5 extends AJExample
{
	public static final String NAME = "BitmapExample 5";
	public static final String DESCRIPTION = "Description";
	public static final String EXAMPLE_SRC = "https://code.google.com/p/gwt-actionjava/source/browse/trunk/aj-examples/aj-examples-bitmap/src/main/java/net/atremmedia/gwt/actionjava/examples/bitmap/samples/BitmapExample5.java";

	private Bitmap testBitmap;
	private Shape testShape;
	private Sprite testSprite;
	private Timer tweenTimer;

	public BitmapExample5(AJExampleOverview base)
	{
		super(NAME, DESCRIPTION, EXAMPLE_SRC, base);
	}

	@Override
	public void run()
	{
		RootPanel.get("ajTest").getElement().getStyle().setBackgroundColor("#000000");

		testSprite = new Sprite();
		testSprite.setX(400);
		testSprite.setY(150);

		testShape = new Shape();
		testShape.setX(0);
		testShape.setY(0);
		Graphics graph = testShape.getGraphics();
		graph.beginStroke("#000");
		graph.beginFill(Color.getRGBA(0xFF0000, 1));
		graph.drawRoundRect(-125, -75, 250, 150, 10);
		graph.closePath();

		testBitmap = new Bitmap(new BitmapData("img/bitmapExample/testImg2.jpg"));
		testBitmap.setX(-150);
		testBitmap.setY(-110);
		testBitmap.setMask(testShape);

		testSprite.addChild(testBitmap);
		addChild(testSprite);

		animate();
		initTimer();
	}

	private void initTimer()
	{
		tweenTimer = new Timer()
		{
			@Override
			public void run()
			{
				animate();
			}
		};
		tweenTimer.scheduleRepeating(5100);
	}

	private void animate()
	{
		Anim.tween(new Manifest(testShape, 3, getRandomEase())
		{
			@Override
			protected void configure()
			{
				fromTo(SCALE_X, 0, 1);
				fromTo(SCALE_Y, 0, 1);
				fromTo(ROTATION, 0, -360);
			}
		});
	}

	@Override
	public void destroy()
	{
		super.destroy();
		tweenTimer.cancel();
	}
}
